package com.ruoyi.settled.helper;

import com.ruoyi.settled.domain.SettlementAgreement;
import com.ruoyi.settled.service.ISettlementAgreementService;
import com.ruoyi.settled.service.IAgreementRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 结算计算器工具类
 */
@Component
public class SettlementCalculator {

    @Autowired
    private ISettlementAgreementService settlementAgreementService;

    @Autowired
    private IAgreementRuleService agreementRuleService;

    /**
     * 计算物流商或供货商的结算金额
     *
     * @param orderAmount 订单金额
     * @param taxRate     税率
     * @return 结算金额
     */
    public static BigDecimal calculateLogisticsOrSupplierSettlement(BigDecimal orderAmount, BigDecimal taxRate) {
        return orderAmount.multiply(BigDecimal.ONE.subtract(taxRate));
    }

    /**
     * 计算团长的结算金额
     *
     * @param orderAmount 订单金额
     * @param commissionRate 提成率
     * @param taxRate     税率
     * @return 结算金额
     */
    public static BigDecimal calculateTeamLeaderSettlement(BigDecimal orderAmount, BigDecimal commissionRate, BigDecimal taxRate) {
        return orderAmount.multiply(commissionRate).multiply(BigDecimal.ONE.subtract(taxRate));
    }

    /**
     * 计算博主的结算金额
     *
     * @param orderAmount 订单金额
     * @param bonusRate   分红率
     * @param taxRate     税率
     * @return 结算金额
     */
    public static BigDecimal calculateBloggerSettlement(BigDecimal orderAmount, BigDecimal bonusRate, BigDecimal taxRate) {
        return orderAmount.multiply(bonusRate).multiply(BigDecimal.ONE.subtract(taxRate));
    }

    /**
     * 计算经销商的结算金额
     *
     * @param orderAmount 订单金额
     * @param discountRate 折扣率
     * @param taxRate     税率
     * @return 结算金额
     */
    public static BigDecimal calculateDealerSettlement(BigDecimal orderAmount, BigDecimal discountRate, BigDecimal taxRate) {
        return orderAmount.multiply(discountRate).multiply(BigDecimal.ONE.subtract(taxRate));
    }

    /**
     * 根据合作方编码计算结算金额
     *
     * @param orderAmount 订单金额
     * @param partnerCode 合作方编码
     * @return 结算金额
     */
    public BigDecimal calculateSettlementByPartnerCode(BigDecimal orderAmount, String partnerCode) {
        String settlementAgreement1 = ISettlementAgreementService.getByPartnerCode(partnerCode);
        if (settlementAgreement1 == null) {
            throw new IllegalArgumentException("结算协议不存在");
        }

        if (partnerCode == null) {
            throw new IllegalArgumentException("合作方编码不存在");
        }

        SettlementAgreement settlementAgreement = settlementAgreementService.selectSettlementAgreementByPartnerCode(settlementAgreement1);
        BigDecimal commissionRate = settlementAgreement.getCommissionRate();
        BigDecimal bonusRate = settlementAgreement.getBonusRate();
        BigDecimal discountRate = settlementAgreement.getDiscountRate();
        String agreementCode = settlementAgreement.getAgreementCode();




        // 检查 agreementRule 是否为 null
        if (agreementCode == null ) {

            throw new IllegalArgumentException("协议规则为空或不存在1");
        }

        com.ruoyi.settled.domain.AgreementRule agreementRule = agreementRuleService.selectAgreementRuleByAgreementCode(agreementCode);

        if (agreementRule == null || agreementRule.isEmpty()) {

            throw new IllegalArgumentException("协议规则为空或不存在2");
        }

        // 获取协议结算公式
        String agreementFormula = agreementRule.getAgreementFormula();
        if (agreementFormula == null || agreementFormula.isEmpty()) {
            throw new IllegalArgumentException("协议结算公式不存在");
        }

        BigDecimal taxRate = agreementRule.getTaxRate();

        if (commissionRate != null) {
            return calculateTeamLeaderSettlement(orderAmount, commissionRate, taxRate);
        } else if (bonusRate != null) {
            return calculateBloggerSettlement(orderAmount, bonusRate, taxRate);
        } else if (discountRate != null) {
            return calculateDealerSettlement(orderAmount, discountRate, taxRate);
        } else {
            throw new IllegalArgumentException("未找到有效的结算规则");
        }
    }
}